﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SimplexEngine
{
    public class RenderContext : IComparable<RenderContext>
    {
        public RenderContext(String context_name, RenderSystem creator)
        {
            name = context_name;
            render_system = creator;
            ViewArea = new ViewArea();
        }

        protected RenderSystem render_system = null;

        public ClearOptions ClearOptions { get { return clear_options; } set { clear_options = value; } }
        protected ClearOptions clear_options = ClearOptions.Target | ClearOptions.DepthBuffer;

        protected int priority = 0;
        public int Priority { get { return priority; } set { priority = value; } }

        protected String name = "";
        public String Name { get { return name; } set { name = value; } }

        public int CompareTo(RenderContext other)
        {
            return priority.CompareTo(other.priority);
        }

        internal protected virtual List<RenderTarget2D> GetRenderTargets()
        {
            return null;
        }

        protected internal virtual List<String> GetTextureNames()
        {
            return null;
        }

        internal protected virtual Texture2D GetTexture(String name)
        {
            return null;
        }

        public virtual void Draw(GameTime game_time, Int64 frame_number)
        {
            // set render target
            render_system.SetRenderContext(this);

            if (view_area != null && view_area.CanDraw)
            {
                // set view port
                Root.Instance.RenderSystem.ViewPort = view_area.ViewPort;
                // draw
                OnDraw(game_time, frame_number);
            }

            OnScreenEffect(game_time, frame_number);
            sprite_manager.Draw(render_system, game_time, frame_number);
        }

        protected virtual void OnDraw(GameTime game_time, Int64 frame_number) 
        {
            render_system.RenderParameters.View = view_area.Camera.ViewMatrix;
            render_system.RenderParameters.Projection = view_area.Camera.ProjectionMatrix;
            // tell scene manager to draw
            view_area.Camera.Parent.Creator.Draw(view_area.Camera, render_system, view_filter, game_time, frame_number);
        }

        protected virtual void OnScreenEffect(GameTime game_time, Int64 frame_number)
        {
            screen_effect_system.Apply(game_time, frame_number);
        }

        public int ViewFilter { get { return view_filter; } set { view_filter = value; } }
        protected int view_filter = (int)SceneObjectFilter.ENTITY;

        protected ScreenEffectSystem screen_effect_system = new ScreenEffectSystem();
        public virtual ScreenEffectSystem ScreenEffectSystem { get { return screen_effect_system; } }

        protected SpriteManager sprite_manager = new SpriteManager();
        public virtual SpriteManager SpriteManager { get { return sprite_manager; } }

        public virtual Color ClearColor { get { return clear_color; } set { clear_color = value; } }
        protected Color clear_color = new Color(0.0f, 0.0f, 0.0f, 0.0f);

        protected ViewArea view_area = null;
        public ViewArea ViewArea 
        { 
            get { return view_area; }
            set { view_area = value; }
        }
    }
}
